<?php

/**
 * Trida modelu pro nemovitosti.
 *
 * @author David Dusek
 */
class EstateModel extends NTableSelection {
    public function __construct(NConnection $connection) {
        parent::__construct("estate", $connection);
    }
    
    /**
     * Smaze nemovitost z dtabaze i s obrazky (z databaze i z disku).
     * @param int $id Id nemovitosti.
     */
    public function deleteWithPictures($id) {
        if ($this->get($id)) {
            Picture::deleteFolder($id);
            $this->where("id", $id)->delete();
        }
    }
    
    /**
     * Deaktivuje nemovitost, ale ponecha ji v databazi.
     * @param type $id 
     */
    public function deactivate($id) {
        if ($this->get($id)) {
            $this->where("id", $id)->update(array("deleted" => 1));
        }
    }
    
    /**
     * Prida podminku pro filtrovani nabidek podle zvoleneho typu nemovitosti.
     * Vrati tiulek kategorie.
     * @param string $type Textovy typ nemovitosti.
     * @return titulek 
     */
    public function setType($type) {
        switch($type) {
            case "domy":
                $cond = "(estate_type = ? OR estate_type = ?) AND (estate_subtype != ? AND estate_subtype != ?)";
                $params = array(2, 3, 4, 5);
                $title = "Domy";
                break;
            case "byty":
                $cond = "estate_type";
                $params = 1;
                $title = "Byty";
                break;
            case "chaty-chalupy":
                $cond = "estate_subtype = ? OR estate_subtype = ?";
                $params = array(4, 5);
                $title = "Chaty a chalupy";
                break;
            case "komercni":
                $cond = "estate_type";
                $params = 4;
                $title = "Komerční";
                break;
            case "pozemky":
                $cond = "estate_type";
                $params = 5;
                $title = "Pozemky";
                break;
            case "jine":
                $cond = "estate_type";
                $title = "Jiné";
                $params = 6;
                break;
            default:
                $cond = null;
                $params= null;
                $title = "Vše";
        }
        
        if ($cond !== null && $params !== null) {
            $this->where($cond, $params);
        }
        
        return $title;
    }
    
    /**
     * Prida podminku pro filtrovani nabidek podle zvoleneho podtypu nemovitosti.
     * Vrati titulek podkategorie.
     * @param string $type Textovy podtyp nemovitosti.
     * @return titulek 
     */
    public function setSubtype($subtype) {
        switch($subtype) {
            case "prodej":
                $cond = "commission_type";
                $params = 1;
                $title = "prodej";
                break;
            case "pronajem":
                $cond = "commission_type";
                $params = 2;
                $title = "pronájem";
                break;
            case "garsoniera":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(1, 2);
                $title = "garsoniera";
                break;
            case "1-kk":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(2, 2);
                $title = "1+kk";
                break;
            case "1-1":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(3, 2);
                $title = "1+1";
                break;
            case "2-kk":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(4, 2);
                $title = "2+kk";
                break;
            case "2-1":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(5, 2);
                $title = "2+1";
                break;
            case "3-kk":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(6, 2);
                $title = "3+kk";
                break;
            case "3-1":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(7, 2);
                $title = "3+1";
                break;
            case "4-kk":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(8, 2);
                $title = "4+kk";
                break;
            case "4-1":
                $cond = "disposition = ? AND commission_type != ?";
                $params = array(9, 2);
                $title = "4+1";
                break;
            case "vetsi":
                $cond = "disposition > ? AND commission_type != ?";
                $params = array(9, 2);
                $title = "větší";
                break;            
            default:
                $cond = null;
                $params= null;
                $title = "vše";
        }
        
        if ($cond !== null && $params !== null) {
            $this->where($cond, $params);
        }
        
        return $title;
    }
}
